package com.qr.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.krui.helper.bean.AppResult;
import cn.krui.helper.tools.CodeUtils;
import cn.krui.helper.tools.MapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author: zc
 * email: zc8023zs@163.com
 * create_time: 2020/8/13 10:20:40
 */
@RestController
@Api(value = "服务说明及错误输出")
@RequestMapping(value = "/")
public class ActuatorController {
	@Value("${server.port}")
	private String port;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String hello_word() throws UnknownHostException {
		final InetAddress addr = InetAddress.getLocalHost();
		final String ip = addr.getHostAddress().toString(); // 获取本机ip
		final String hostName = addr.getHostName().toString(); // 获取本机计算机名称
		final String returnStr = "您访问的是：【端口号】" + port + ",【主机名】" + hostName + ",【IP】" + ip;
		return returnStr;
	}

	@RequestMapping(value = "/actuator/info", method = RequestMethod.GET)
	@ApiOperation(value = "获取服务名称和端口", notes = "获取服务名称和端口", httpMethod = "GET")
	public String helloword() throws UnknownHostException {
		final InetAddress addr = InetAddress.getLocalHost();
		final String ip = addr.getHostAddress().toString(); // 获取本机ip
		final String hostName = addr.getHostName().toString(); // 获取本机计算机名称
		final String returnStr = "您访问的是：【端口号】" + port + ",【主机名】" + hostName + ",【IP】" + ip;
		return returnStr;
	}

	@RequestMapping(value = "/actuator/printf")
	@ApiOperation(value = "输出错误信息", httpMethod = "GET")
	@ResponseBody
	public AppResult<String> printf(HttpServletRequest request) {
		AppResult<String> app = new AppResult<String>();
		Object codeMap = request.getAttribute("data");
		if (codeMap == null){
			return app;
		}
		Map<String, Object> data = (Map<String, Object>) codeMap;
		if (MapUtils.getInt(data, "code") == (CodeUtils.LOGIN_TIMEOUT)) {
			app.setCode(CodeUtils.LOGIN_TIMEOUT);
			app.setMsg("登录超时");
			return app;
		}
		if (MapUtils.getInt(data, "code") == (CodeUtils.PERMISSSION_OUT)) {
			app.setCode(CodeUtils.PERMISSSION_OUT);
			app.setMsg("没有权限");
			return app;
		}

		return app;
	}
}
